import { loginApi, userInfoApi } from "@/services/api/auth";

export const useUserStore = defineStore("user", {
  persist: true, //持久化
  state: () => ({
    token: "",
    user: null,
  }),
  actions: {
    /**
     * 登录
     */
    login(data) {
      const { user_login_name, mm } = data;
      return new Promise((resolve, reject) => {
        loginApi(user_login_name.trim(), mm)
          .then((res) => {
            this.token = res.data.token;
            resolve(res.data.token);
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
    /**
     *  获取用户信息（昵称、头像、角色集合、权限集合）
     */
    getUserInfo() {
      return new Promise((resolve, reject) => {
        const userMenu = useMenuStore();
        userInfoApi()
          .then((res) => {
            this.token = res.data.token;
            this.user = {
              name: res.data.xingm,
              id: res.data.personnel_info_id,
              bum: res.data.bum,
              bumid: res.data.bumid,
              avatar: res.data.avatar,
              sign_picture_url: res.data.sign_picture_url,
            };
            userMenu.setMenu(["ADMIN"]);
            resolve({});
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
  },
});
